﻿using System;
using System.Net.Http;
using System.Windows.Forms;
using System.IO;
using System.Media;
using TextEditApp.Algorithm;

namespace TextEditApp.Forms
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
            //this.skinEngine1.SkinFile = "../../Skincontrol/Skins/XPGreen.ssk";
        }
        private string content;
        public string Content
        {
            get
            {
                return content;
            }
            set
            {
                content = value;
                tbx_main.Text = content;
                //Content = value;
            }
        }

        private void openFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            var result = dialog.ShowDialog();
            if (result != DialogResult.OK)
                return;
            string path = dialog.FileName;
            string text = File.ReadAllText(path);
            tbx_main.Text = text;
            lbl_path.Text = path;
        }

        private void saveFile_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(lbl_path.Text))
            {
                SaveFileDialog dialog = new SaveFileDialog();
                var result = dialog.ShowDialog();
                if (result != DialogResult.OK)
                    return;
                string path = dialog.FileName;
                File.WriteAllText(path, tbx_main.Text);
            }
            else
            {
                File.WriteAllText(lbl_path.Text, tbx_main.Text);
            }
            MessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void saveAs_Click(object sender, EventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            var result = dialog.ShowDialog();
            if (result != DialogResult.OK)
                return;
            string originPath = lbl_path.Text;
            string tagPath = dialog.FileName;
            File.WriteAllText(tagPath, tbx_main.Text);
            File.Delete(originPath);
        }

        private void newFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tbx_main.Text = string.Empty;
            lbl_path.Text = string.Empty;
        }

        private void fontToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FontDialog dialog = new FontDialog();
            var result = dialog.ShowDialog();
            if (result != DialogResult.OK)
            {
                return;
            }
            tbx_main.Font = dialog.Font;
        }

        private void autoWrapToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tbx_main.WordWrap = !tbx_main.WordWrap;
            autoWrapToolStripMenuItem.Checked = tbx_main.WordWrap;
        }

        private void findToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FindForm form = new FindForm();
            form.Show(this);
        }

        private void replaceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ReplaceForm form = new ReplaceForm();
            form.Show(this);
        }

        private void singleWordToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SingleCountForm form = new SingleCountForm();
            form.Show(this);
        }

        private void countWholeFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CountResult form = new CountResult();
            form.Show(this);
        }

        private void eventToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("123");
            Helpevent form = new Helpevent();
            form.Show(this);
        }

        private void readToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string text = tbx_main.Text;
            string url = $"https://api.oick.cn/txt/apiz.php?text={text}&spd=5";
            HttpClient client = new HttpClient();
            HttpClientHandler handler = new HttpClientHandler();
            //handler.
            var result = client.GetAsync(url).Result.Content.ReadAsStreamAsync().Result;
            string filePath = SaveMp3(result);
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.SoundLocation = filePath;
            player.Load();
            player.Play();
        }

        private string SaveMp3(Stream fileStream)
        {
            string path = $@"C:\Users\79175\Downloads\{DateTime.Now.ToString("yyyyMMddHHmmss")}.mp3";
            using (var stream = File.Create(path))
            {
                fileStream.Seek(0, SeekOrigin.Begin);
                fileStream.CopyTo(stream);
            }
            return path;
        }

        private void SaveToDisk(byte[] bytes, string saveFullPath)
        {
            var fullPath = Path.GetDirectoryName(saveFullPath);
            //如果没有此文件夹，则新建
            if (!Directory.Exists(fullPath))
            {
                Directory.CreateDirectory(fullPath);
            }
            //创建文件，返回一个 FileStream，它提供对 path 中指定的文件的读/写访问。
            using (FileStream stream = File.Create(saveFullPath))
            {
                //将字节数组写入流
                stream.Write(bytes, 0, bytes.Length);
                stream.Close();
            }
        }
        //快捷键绑定
        //private void Main_KeyDown(object sender, KeyEventArgs e)
        //{
        //    if (e.KeyCode == Keys.F && e.Control)
        //    {
        //       e.Handled = true;       //将Handled设置为true，指示已经处理过KeyDown事件   
        //       editToolStripMenuItem.PerformClick(); //执行单击button1的动作   
        //   }
        //}

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            // 实现按Enter键来实现Ctrl+S键的功能
            //if (keyData == Keys.Enter)
            //{
            //    SendKeys.Send("^F");
            //    return true;
           // }
            // 按快捷键Ctrl+S执行按钮的点击事件方法
            //if (keyData == (Keys.Control | Keys.S))
            if (keyData == (Keys)Shortcut.CtrlF)
            {
                findToolStripMenuItem.PerformClick();
                return true;
            }
            if (keyData == (Keys)Shortcut.CtrlH)
            {
                replaceToolStripMenuItem.PerformClick();
                return true;
            }
            
            if (keyData == (Keys)Shortcut.CtrlS)
            {
                saveFile.PerformClick();
                return true;
            }
            if (keyData == (Keys)Shortcut.CtrlO)
            {
                openFile.PerformClick();
                return true;
            }
            if (keyData == (Keys)Shortcut.CtrlN)
            {
                newFileToolStripMenuItem.PerformClick();
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData); // 其他键按默认处理　

        }



        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void tbx_main_TextChanged(object sender, EventArgs e)
        {

        }

        private void Main_Load(object sender, EventArgs e)
        {

        }
    }
}
